package logic

import (
	"context"
	"shopping-mall/shopping_rpc/shoppingrpcclient"

	"shopping-mall/api/internal/svc"
	"shopping-mall/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProductLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProductLogic {
	return &AddProductLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddProductLogic) AddProduct(req *types.AddProductRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	product, err := l.svcCtx.ShopRpc.AddProduct(l.ctx, &shoppingrpcclient.AddProductRequest{
		UserId:  req.UserID,
		Title:   req.Title,
		Price:   req.Price,
		Image:   req.Image,
		ClassId: req.ClassID,
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "商品发布成功",
		Data:    product,
	}, nil
}
